<?php

namespace Home\Controller;

/**
 * 代理商
 *
 * @author Administrator
 *        
 */
class AgentController extends BaseController {
	/**
	 * 管理员列表
	 */
	public function mb_GetAgentList() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 0) {
			// 总管理员
			$adminDatas = $adminInfo->where ( "level != '0' AND level!='1'" )->select ();
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			$adminDatas = $adminInfo->where ( "code like '" . $subCode . "___' AND level != '2'" )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $adminDatas ) );
	}
	/**
	 * 更新管理员状态
	 */
	public function mb_UpdateAdminStatus() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		$status = $data->status;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$adminInfo->save ( array (
				'id' => $id,
				'status' => $status 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 更新管理员信息
	 */
	public function mb_UpdateAdminInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		$nick = $data->nick;
		$basePass = $data->pass;
		$encPasss = md5 ( $basePass . C ( 'MSG_MD5' ) );
		$phone = $data->phone;
		$upCoin = $data->upcoin;
		$accountShow = $data->accountshow;
		$status = $data->status;
		$agent = $data->agent;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		$agentData = $adminInfo->where ( "id='$id'" )->find ();
		if ($agentData ['sw_upcoin'] != $upCoin) {
			if ($agentData ['level'] == 2) {
				$subCode = mb_substr ( $agentData ['code'], 0, 5 );
				$where = "code like '" . $subCode . "___'";
			} else if ($agentData ['level'] == 3) {
				$code = $agentData ['code'];
				$where = "code = '$code'";
			}
			$coins = $userInfo->where ( $where )->sum ( 'coin' );
			if ($coins == '')
				$coins = 0;
			
			if ($coins != 0) {
				$this->mb_OutMessage ( ErrCode_Toast, '旗下玩家分数没清零，上下分权限暂无更改' );
				return;
			}
			
			if ($agentData ['level'] == 2) {
				$subCode = mb_substr ( $agentData ['code'], 0, 5 );
				$adminInfo->where ( "code like '" . $subCode . "___'" )->save ( array (
						'sw_upcoin' => $upCoin 
				) );
			}
		}
		
		$adminInfo->save ( array (
				'id' => $id,
				'nick' => $nick,
				'basepass' => $basePass,
				'encpass' => $encPasss,
				'phone' => $phone,
				'sw_upcoin' => $upCoin,
				'sw_accountshow' => $accountShow,
				'sw_agent' => $agent,
				'status' => $status 
		) );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	
	/**
	 * 充值操作（上分，下分，送分）
	 */
	public function mb_RechargeHandle() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$id = $data->id;
		$type = $data->type; // 0：上分，1：下分，2：送分
		$coin = $data->coin;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$adminId = $adminData ['id'];
		$adminCoin = $adminData ['coin'];
		$adminLevel = $adminData ['level'];
		$handlerName = $adminData ['name'];
		$handlerCode = $adminData ['code'];
		
		$agentData = $adminInfo->where ( "id='$id'" )->find ();
		$agentName = $agentData ['name'];
		$agentCoin = $agentData ['coin'];
		$agentCode = $agentData ['code'];
		$newCoin = $agentCoin + $coin;
		
		if ($newCoin < 0) {
			$this->mb_OutMessage ( ErrCode_Toast, '代理额度不足' );
			return;
		}
		
		if ($adminLevel == 2 || $adminLevel == 3) {
			if ($type == 0 || $type == 1 || $type == 2) {
				$adminNewCoin = $adminCoin - $coin;
				if ($adminNewCoin < 0) {
					$this->mb_OutMessage ( ErrCode_Toast, '当前额度不足！' );
					return;
				}
				$adminInfo->save ( array (
						'id' => $adminId,
						'coin' => $adminNewCoin 
				) );
			}
		}
		
		$adminInfo->save ( array (
				'id' => $id,
				'coin' => $newCoin 
		) );
		$adminRechargeLog = M ( 'Adminrechargelog' );
		$adminRechargeLog->add ( array (
				'adminname' => $agentName,
				'beforecoins' => $agentCoin,
				'coins' => $coin,
				'latercoins' => $newCoin,
				'type' => $type,
				'code' => $agentCode,
				'handlername' => $handlerName,
				'handlercode' => $handlerCode 
		) );
		$this->mb_OutMessage ( ErrCode_Normal, ( string ) $newCoin );
	}
	
	/**
	 * 获取上下分信息
	 */
	public function mb_GetUpDownCoinInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$adminRechargeLog = M ( 'Adminrechargelog' );
		if ($date1 != '' && $date2 != '') {
			$adminRechargeData = $adminRechargeLog->where ( "adminname='$name' AND date>'$date1' AND date<'$date2'" )->order ( 'id desc' )->select ();
		} else {
			$adminRechargeData = $adminRechargeLog->where ( "adminname='$name'" )->order ( 'id desc' )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $adminRechargeData ) );
	}
	/**
	 * 获取代理消费信息
	 */
	public function mb_GetAdminConsumptionInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$code = $data->code;
		
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userRechargeLog = M ( 'Userrechargelog' );
		if ($date1 == $date2) {
			$userRechargeData = $userRechargeLog->where ( "code='$code' AND date>='$date1'" )->select ();
		} else {
			$userRechargeData = $userRechargeLog->where ( "code='$code' AND date>='$date1' AND date<='$date2'" )->order ( 'id desc' )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userRechargeData ) );
	}
	/**
	 * 获取玩家消费信息
	 */
	public function mb_GetUserConsumptionInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$name = $data->name;
		
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userRechargeLog = M ( 'Userrechargelog' );
		if ($date1 == $date2) {
			$userRechargeData = $userRechargeLog->where ( "username='$name' AND date>='$date1'" )->select ();
		} else {
			$userRechargeData = $userRechargeLog->where ( "username='$name' AND date>='$date1' AND date<='$date2'" )->order ( 'id desc' )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userRechargeData ) );
	}
	/**
	 * 代理商统一设置
	 */
	public function UnifiedSetting() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$status = $data->status;
		$account = $data->account;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		if ($adminData ['level'] == 0) {
			// 总管理员
			$adminDatas = $adminInfo->where ( "level != '0'" )->select ();
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			$adminDatas = $adminInfo->where ( "code like '" . $subCode . "___' AND level != '2'" )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		
		for($i = 0; $i < count ( $adminDatas ); $i ++) {
			$id = $adminDatas [$i] ['id'];
			$adminInfo->save ( array (
					'id' => $id,
					'status' => $status,
					'sw_accountshow' => $account 
			) );
		}
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}

    public function mb_SetOnOfflineRealtime()
    {
        $data = $this->mb_OnMessage();
        $token = $data->token;
        $adminId = $data->adminid;
        $switch = $data->switch;

        $ret = $this->mb_CheckAdminIsLoggedIn($token);
        $adminInfo = $ret->info;
        $adminData = $ret->data;

        $adminInfo->save(array('id'=>$adminId,'onoffline_realtime'=>$switch));
        $this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
    }
}